// Album.cs
// Facebook/Framework/Service
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Facebook.Service.Core;

namespace Facebook.Service {

    /// <summary>
    /// Represents a Facebook photo album.
    /// </summary>
    public sealed class Album : FacebookEntity {

        private bool _idTransformed;
        private bool _ownerTransformed;
        private bool _coverIDTransformed;
        private bool _createdDateTransformed;
        private bool _modifiedDateTransformed;
        private bool _locationTransformed;

        internal Album(Hashtable albumState)
            : base(albumState) {
        }

        /// <summary>
        /// The unique ID of the cover photo.
        /// </summary>
        public string CoverID {
            get {
                if (_coverIDTransformed == false) {
                    return TransformCoverID();
                }
                return (string)this["cover_pid"];
            }
        }

        /// <summary>
        /// The description of this photo album.
        /// </summary>
        public string Description {
            get {
                return GetState<string>("description") ?? String.Empty;
            }
        }

        /// <summary>
        /// The date this album was created.
        /// </summary>
        public DateTime CreatedDate {
            get {
                if (_createdDateTransformed == false) {
                    return TransformCreatedDate();
                }
                return (DateTime)this["created"];
            }
        }

        /// <summary>
        /// The number of photos in this album.
        /// </summary>
        public int Count {
            get {
                return GetState<int>("size");
            }
        }

        /// <summary>
        /// The unique ID of the album.
        /// </summary>
        public string ID {
            get {
                if (_idTransformed == false) {
                    return TransformID();
                }
                return (string)this["aid"];
            }
        }

        /// <summary>
        /// The url associated with the album.
        /// </summary>
        public string LinkUrl {
            get {
                return GetState<string>("link") ?? String.Empty;
            }
        }

        /// <summary>
        /// The location associated with this album.
        /// </summary>
        public Location Location {
            get {
                if (_locationTransformed == false) {
                    return TransformLocation();
                }
                return (Location)this["location"];
            }
        }

        /// <summary>
        /// The date this album was modified.
        /// </summary>
        public DateTime ModifiedDate {
            get {
                if (_modifiedDateTransformed == false) {
                    return TransformModifiedDate();
                }
                return (DateTime)this["modified"];
            }
        }

        /// <summary>
        /// The name of this photo album.
        /// </summary>
        public string Name {
            get {
                return GetState<string>("name") ?? String.Empty;
            }
        }

        /// <summary>
        /// The unique ID of the user who owns this album.
        /// </summary>
        public string OwnerID {
            get {
                if (_ownerTransformed == false) {
                    return TransformOwner();
                }
                return (string)this["owner"];
            }
        }

        private string TransformCoverID() {
            Debug.Assert(_coverIDTransformed == false);

            string id = FacebookResponse.CoerceString(this["cover_pid"]);

            this["cover_pid"] = id;
            _coverIDTransformed = true;

            return id;
        }

        private DateTime TransformCreatedDate() {
            Debug.Assert(_createdDateTransformed == false);

            DateTime createdDate = FacebookResponse.CoerceDateTime(this["created"]);

            this["created"] = createdDate;
            _createdDateTransformed = true;

            return createdDate;
        }

        private string TransformID() {
            Debug.Assert(_idTransformed == false);

            string id = FacebookResponse.CoerceString(this["aid"]);

            this["aid"] = id;
            _idTransformed = true;

            return id;
        }

        private Location TransformLocation() {
            Debug.Assert(_locationTransformed == false);

            Hashtable locationState = (Hashtable)this["location"];
            Location location = new Location(locationState);

            this["location"] = location;
            _locationTransformed = true;

            return location;
        }

        private DateTime TransformModifiedDate() {
            Debug.Assert(_modifiedDateTransformed == false);

            DateTime modifiedDate = FacebookResponse.CoerceDateTime(this["modified"]);

            this["modified"] = modifiedDate;
            _modifiedDateTransformed = true;

            return modifiedDate;
        }

        private string TransformOwner() {
            Debug.Assert(_ownerTransformed == false);

            string id = FacebookResponse.CoerceString(this["owner"]);

            this["owner"] = id;
            _ownerTransformed = true;

            return id;
        }
    }
}
